<?php

//    error_reporting(E_ALL);
//    ini_set('display_errors', '1');

    require_once "../lib/WxPay.Api.php";
    require_once "WxPay.JsApiPay.php";
    require_once 'log.php';
    require_once 'mysql.func.php';
    $db_package = "cmf_wl_package";

    $con = new MyConnect();
    $package_id = isset($_GET['package_id'])?(int)$_GET["package_id"]:"";
    $package = $con->selectData($db_package,array("package_id"=>$package_id));

    //初始化日志
    $logHandler = new CLogFileHandler("../logs/" . date('Y-m-d') . '.log');
    $log = Log::Init($logHandler, 15);

    //①、获取用户openid
    $tools = new JsApiPay();
    $openId = $tools->GetOpenid();





    //②、统一下单
    $input = new WxPayUnifiedOrder();
    $input->SetBody('邦客邻里');
    $input->SetAttach('SetAttach');

    $input->SetOut_trade_no("12312321312312");
    $input->SetTotal_fee(1);//200000
    $input->SetTime_start(date("YmdHis"));
    $input->SetTime_expire(date("YmdHis", time() + 600));
    $input->SetGoods_tag("邦客邻里");
    $input->SetNotify_url("http://bkll.dotry1.cn/wechat_pay/example/notify.php");
    $input->SetTrade_type("JSAPI");
    $input->SetOpenid($openId);
    $order = WxPayApi::unifiedOrder($input);

    $jsApiParameters = $tools->GetJsApiParameters($order);



/**
 * 订单号生成函数
 * 选择一个随机的方案
 * @return int
 */
function build_order_no() {
    mt_srand((double) microtime() * 1000000);
    return date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
    <title>微信支付</title>
    <style>
        .pay-head{ position: absolute;top:0px;left:0px;right:0px;background-color:#E0E1DC; text-align:center;padding-top:40px;border-bottom:3px #D4D4D2 solid;}
        .pay-head h{ font-family:"黑体";font-size:40px;margin-bottom:20px;height:60px;line-height:60px;}
        .pay-head p{margin-top:40px;margin-bottom:40px;height:100px;line-height:100px;}
        .pay-head p b{font-family:"黑体";font-size:60px}
        .pay-head p span{font-family: Arial, Helvetica, sans-serif;font-size:120px}
        .pay-content{position:absolute;top:300px;right:0px;left:0px;border-bottom:3px #D4D4D2 solid;}
        .pay-content p{margin:50px;font-size:40px;font-family:"黑体";height:60px;line-height:60px;}
        .pay-content p span:first-child{color:#828282;}
        .pay-right{float:right;}
        .pay-buttom{position:absolute;top:670px;right:0px;left:0px;text-align:center;padding-top:60px;}
        .pay-buttom button{outline:none;height:120px;width:80%;background:#1BA625;border-radius:10px;font-size:70px;line-height:120px;color:#ffffff;}
    </style>
</head>
<script type="text/javascript">
    //调用微信JS api 支付
    function jsApiCall()
    {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            <?php echo $jsApiParameters; ?>,
            function (res) {
                WeixinJSBridge.log(res.err_msg);
                alert("支付成功！");
            }
        );
    }

    function callpay()
    {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        } else {
            jsApiCall();
        }
    }
</script>
<body>
<div class="pay-head">
    <h><?php echo $title;?></h>
    <p><b>￥</b><span><?php echo $price;?></span></p>
</div>
<div class="pay-content">
    <p><span>收款方</span><span class="pay-right">苏州邦客邻里电器有限公司</span></p>
    <p><span>订  单</span><span class="pay-right"><?php echo $trade_no; ?></span></p>
</div>
<div  class="pay-buttom" onclick="callpay()" >
    <button>立即支付</button>
</div>
</body>
</html>